﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using OttawaTourMap.Models;
using System.Windows.Navigation;
using Expression.Blend.SampleData.ottawaSampleDataSource;
using OttawaTourMap.Resources.Localization;
using System.Windows.Threading;

namespace OttawaTourMap
{
    public partial class TourDetailsPanoramicView : PhoneApplicationPage
    {
        private string tourID;
        DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
        public TourDetailsPanoramicView()
        {
            InitializeComponent();
            //LocalizeStrings();
            tourDetailsModel = new TourDetailsModel();
            imageModel = new ImagesModel();

            InitializeAnimationThread();
        }

        private void InitializeAnimationThread()
        {
            //assign the event handler to the tick event
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);

            //choose how often should the readings be taken
            dispatcherTimer.Interval = new TimeSpan(0, 0, 1);

            //what to do when the reading has been taken (don't forget, all Silverlight calls are ASync, so you need to handle
            //the completed event to get the result
            // client.GetGPSDataCompleted += new EventHandler<ServiceReference1.GetGPSDataCompletedEventArgs>(client_GetGPSDataCompleted);

            //start the timer
            dispatcherTimer.Start();
        }

        void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            //Iterate thorugh the tour details Model and change the image uri for each stop info
            int i = 0;
            int max = 0;
            
            foreach (var tourDetails in tourDetailsModel.TourDetails)
            {
                max = imageCollection.ElementAt(i).Count();
                Random random = new Random();
                tourDetails.TourStopImageURI = imageCollection.ElementAt(i).ElementAt(random.Next(max));
            }

        }

        private void LocalizeStrings()
        {
            TourDetailPanorama.Title = Labels.Tour_Details;
            Stops_Item.Header = Labels.Stops;
            Description_Item.Header = Labels.Description;
        }

        //Stores the descritpion of the tour 
        public string tourDescription = null;

        //This is the data context for the tour details list
        public TourDetailsModel tourDetailsModel = null;

        //This stores all the images for the pictures panorama item
        public ImagesModel imageModel = null;

        //This stores all the images for stop panorama item image animation
        List<List<string>> imageCollection = new List<List<string>>();


        //over ride the on navigate to to extract the details of the tour page
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string s = "";
            if (NavigationContext.QueryString.TryGetValue("tourIndex", out s))
            {
                tourID = s;
                var tours = (Expression.Blend.SampleData.ottawaSampleDataSource.toursList)App.Current.Resources["OttawaSampleDataSource"];
                //Get the selected tour inforamtion from the id
                //Iterate through the collection
                Expression.Blend.SampleData.ottawaSampleDataSource.tour selectedTour = null;
                foreach (var tour in tours.tourCollection)
                {
                    if (tour.id.Equals(tourID))
                    {
                        selectedTour = tour;
                        break;
                    }
                }

                //Once we get the tour Id, get all the inforamtion and store that in the model
                /*
                 * 1. The tour details list is displayed as list of stops . Each entry in the list will contain an image followed by the stop title.
                 * The images will be any of the image from the multiple point of interest
                 */

                //Get the stop collection
                stopCollection stops = selectedTour.stopCollection;

                //Iterate thorugh and then get all the required inforamtion
                foreach (var stop in stops)
                {
                    string stopTitle, thumbNailUri;
                    stopTitle = stop.title;
                    thumbNailUri = stop.pointOfInterestCollection[0].ThumbNailURI;  //Get the image from the first point of interest

                    tourDetailsModel.addToCollection(new TourDetails(stopTitle, thumbNailUri,"Duration :" + stop.duration.ToString() + " mins"));

                    //Build the data for the pictures tab
                    //Iterate Through all Point of interests and collect all the images
                    List<string> thumbNails = new List<string>();
                    foreach (var poi in stop.pointOfInterestCollection)
                    {
                        //Iterate through all images
                        thumbNails.Add(poi.ThumbNailURI);
                        foreach (var image in poi.imageCollection)
                        {
                            imageModel.addImageToCollection(new ImageURI(image.imageURI, image.title));
                            thumbNails.Add(image.imageURI);
                        }
                        

                    }

                    imageCollection.Add(thumbNails);
                }

                tourDescription = selectedTour.description;

                picturesGrid.DataContext = imageModel;
                TourDetailsGrid.DataContext = tourDetailsModel;
                TourDescriptionTextBlock.Text = tourDescription;

                //TourDetailPanorama.Title = "Tour Details";
            }
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
            //Call the page to display the details of the selected stop
            //Do nothing if there is no selection
            if (StopListBox.SelectedIndex == -1)
                return;

            //Navigate to details page and inform the selection
            NavigationService.Navigate(new Uri("/StopDetailsPage_new.xaml?tourIndex=" + tourID + "&stopIndex=" + StopListBox.SelectedIndex, UriKind.Relative));
            //Reset to no selection
            StopListBox.SelectedIndex = -1;
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            //Navigate to details page and inform the selection
            NavigationService.Navigate(new Uri("/MainPage.xaml?tourIndex=" + tourID, UriKind.Relative));

        }

        private void ApplicationBarIconButton_Click_Back(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }
    }
}